function replace_all(Source,stringToFind,stringToReplace){
    var temp = Source;
    var index = temp.indexOf(stringToFind);
    while(index != -1){
        temp = temp.replace(stringToFind,stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}


function uploadIcon(BtnUpload, SubmitVar, contentId ,Status, image_url, base_path){
    var dir_name = BtnUpload.attr('name');
    var time = (new Date).getTime();
    new AjaxUpload((BtnUpload), {
        action: '?r=upload',
        name: SubmitVar,
        data: {
            r: 'upload',
            name: dir_name,
        },
        responseType: false,
        onSubmit: function(file, ext){
            if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
                alert("Chi cho phép file jpg, png");
            }
            Status.text('Đang tải ... ');
        },
        onComplete: function(file, response){
            Status.text('');
            if(response != 'false'){
                var src = replace_all(response,' ','');
                contentId.attr('src',src);
                image_url.val(src);
                //contentId.append('<img id="'+SubmitVar+'_img" src="'+src+'" style="width:200px;height:200px" ></img>');
            }else{
                alert('Upload fail');
            }
        }
    });
}

function uploadThumb(BtnUpload, SubmitVar, contentId ,Status, image_url, base_path){
    var dir_name = BtnUpload.attr('name');
    var time = (new Date).getTime();
    new AjaxUpload((BtnUpload), {
        action: '?r=upload',
        name: SubmitVar,
        data: {
            r: 'upload',
            name: dir_name,
        },
        responseType: false,
        onSubmit: function(file, ext){
            if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
                alert("Chi cho phép file jpg, png");
            }
            Status.text('Đang tải ... ');
        },
        onComplete: function(file, response){
            Status.text('');
            if(response != 'false'){
                var src = replace_all(response,' ','');
                contentId.attr('src',src);
                image_url.val(src);
                //contentId.append('<img id="'+SubmitVar+'_img" src="'+src+'" style="width:200px;height:200px" ></img>');
            }else{
                alert('Upload fail');
            }
        }
    });
}

function uploadWithAppend(BtnUpload, SubmitVar, contentId,Status, contentUrlId, base_path){
    var dir_name = BtnUpload.attr('name');
    var time = (new Date).getTime();
    new AjaxUpload((BtnUpload), {
        action: '?r=upload',
        name: SubmitVar,
        data: {
            r: 'upload',
            name: dir_name,
        },
        responseType: false,
        onSubmit: function(file, ext){
            if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
                alert("Chi cho phép file jpg, png");
            }
            Status.text('Đang tải ... ');
        },
        onComplete: function(file, response){
            Status.text('');
            if(response != 'false'){
                var src = replace_all(response,' ','');
                var url_string = contentUrlId.val();
                url_string = url_string + src+';';
                contentUrlId.val(url_string);
                //alert(url_string);
                image = '<div id="'+src+'" style="float:left;margin:10px 10px 0px 0px;width:100px;">';
                image +='<img src="'+src+'" class="screenshot_image" style="width:100px;height:100px;" ></img>';
                image +='<a href="javascript:void(0)" title="'+src+'" onclick="removeScreenshot(this.title);">Remove</a>';
                image +='</div>';
                contentId.append(image);
            }else{
                alert('Upload fail');
            }
        }
    });
}

function uploadImageInContent(BtnUpload, SubmitVar, contentId,Status, contentUrlId, base_path){
    var dir_name = BtnUpload.attr('name');
    var time = (new Date).getTime();
    new AjaxUpload((BtnUpload), {
        action: '?r=upload',
        name: SubmitVar,
        data: {
            r: 'upload',
            name: dir_name,
        },
        responseType: false,
        onSubmit: function(file, ext){
            if (! (ext && /^(jpg|png|jpeg|bmp|gif)$/.test(ext))){
                alert("Chi cho phép file jpg, png");
            }
            Status.text('Đang tải ... ');
        },
        onComplete: function(file, response){
            Status.text('');
            if(response != 'false'){
                var src = replace_all(response,' ','');
                //var url_string = contentUrlId.val();
                //url_string = url_string + src+';';
                //contentUrlId.val(url_string);
                //alert(url_string);
                image = '<div id="'+src+'" style="float:left;margin:10px 10px 0px 0px;width:100px;">';
                image +='<img src="'+src+'" class="screenshot_image" style="width:100px;height:100px;" ></img>';
                image +='<a href="javascript:void(0)" title="'+src+'" onclick="removeImageInContent(this.title);">Remove</a><input style="width:100px;" value="'+src+'"/>';
                image +='</div>';
                contentId.append(image);
            }else{
                alert('Upload fail');
            }
        }
    });
}

//function removeScreenshot(file_name){
//    var url_string = $('#screenshots_url').val();
//    url_string = replace_all(url_string,file_name+';','');
//    $('#screenshots_url').val(url_string);
//    element = document.getElementById(file_name);
//    element.parentNode.removeChild(element);
//}
function removeImageInContent(file_name){
    dataString = 'file_name='+file_name;
    if(confirm("Bạn có chắc muốn xóa ảnh này không?")) {

        $.ajax({
            type: 'post',
            url: '?r=upload',
            data: dataString,
            dataType: "html",
            success: function(data){
                if(data != 'error'){
                    //var url_string = $('#screenshots_url').val();
                    //url_string = replace_all(url_string,file_name+';','');
                    //$('#screenshots_url').val(url_string);
                    element = document.getElementById(file_name);
                    element.parentNode.removeChild(element);
                }
            }
        });
    }
    else{
        return false;
    }
}



function removeScreenshot(file_name){
    dataString = 'file_name='+file_name;
    if(confirm("Bạn có chắc muốn xóa ảnh này không?")) {

        $.ajax({
            type: 'post',
            url: '?r=upload',
            data: dataString,
            dataType: "html",
            success: function(data){
                if(data != 'error'){
                    var url_string = $('#screenshots_url').val();
                    url_string = replace_all(url_string,file_name+';','');
                    $('#screenshots_url').val(url_string);
                    element = document.getElementById(file_name);
                    element.parentNode.removeChild(element);
                }
            }
        });
    }
    else{
        return false;
    }
}

function uploadFile(BtnUpload, SubmitVar, contentId,Status ){

    //var dir_name = '';

    //dir_name = Name.val();

    new AjaxUpload((BtnUpload), {
        action: '?r=upload',
        name: SubmitVar,
        data: {
            r: 'upload',

        },
        responseType: false,
        onSubmit: function(file, ext){

            Status.text('Đang tải ... ');
        },
        onComplete: function(file, response){
            Status.text('');
            if(response != 'false'){
                var src = replace_all(response,' ','');
                //var url_string = contentUrlId.val();
                //url_string = url_string + src+';';
                //contentUrlId.val(url_string);
                //alert(url_string);
                data = '-<input style="display:none;" type="text" name="profile_link" value="'+src+'" /><label>Đã upload thành công file: </label><label>'+src+'</label>';
                document.getElementById(contentId).innerHTML = '';
                document.getElementById(contentId).innerHTML = data;
            }else{
                alert('Upload fail');
            }
        }
    });

}


$(document).ready(function(){
    uploadImageInContent( $("#images"), 'image', $("#images_content"),$('#images_status') ,$("#images_url"),'http://localhost/vqm/admin.gomo.vn/uploads/images/screenshots/');
    uploadThumb( $("#thumb"), 'image', $("#thumb_content"), $('#thumb_status'), $('#thumb_url'),'http://localhost/vqm/admin.gomo.vn/uploads/images/thumbs/' );
    uploadIcon( $("#icon"), 'image', $("#icon_content"), $('#icon_status'),$('#icon_url'),'http://localhost/vqm/admin.gomo.vn/uploads/images/icons/');
    uploadWithAppend( $("#screenshot"), 'image', $("#screenshots_content"),$('#screenshots_status') ,$("#screenshots_url"),'http://localhost/vqm/admin.gomo.vn/uploads/images/screenshots/');
    //uploadImageInContent( $("#images"), 'image', $("#images_content"),$('#images_status') ,$("#images_url"),'http://localhost/vqm/admin.gomo.vn/uploads/images/screenshots/');


    uploadFile($("#profile"), 'profile', 'source_file_url', $("#upfile_status"));
});
